//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;

namespace OpenLS.Spreadsheet
{
    class CellValueCounter
    {
        public CellValueCounter(Range range)
        {            
            valueCounts = new Dictionary<object, int>();
            foreach (var cell in range.GetNonEmptyCells())
            {
                if (cell.Value == null)
                    continue;
                int count = 0;
                if (valueCounts.TryGetValue(cell.Value, out count))
                {
                    valueCounts.Remove(cell.Value);
                }
                count++;
                valueCounts.Add(cell.Value,count);
            }
        }

        private readonly Dictionary<object, int> valueCounts;

        internal bool IsUnique(object value)
        {
            if (value == null)
                return false;
            int count;
            if (!valueCounts.TryGetValue(value, out count))
                return false;
            return count == 1;
        }

        internal bool IsDuplicate(object value)
        {
            if (value == null)
                return false;
            int count;
            if (!valueCounts.TryGetValue(value, out count))
                return false;
            return count > 1;
        }
    }
}
